package com.solution.doublepointer;

import java.util.Arrays;

/*
    双指针
 */
public class Solution {

    public int findRadius(int[] houses, int[] heaters) {
        Arrays.sort(houses);
        Arrays.sort(heaters);
        int res = 0;
        for ( int i = 0, j = 0 ; i < houses.length ; i++ ) {
            int closest = abs( houses[i] - heaters[j] );
            while ( j < heaters.length - 1 && abs( houses[i] - heaters[j] ) >= abs( houses[i] - heaters[j+1] ) ){
                j++;
                closest = min(closest, abs(houses[i] - heaters[j]));
            }
            res = max(res, closest);
        }
        return res;
    }

    public int abs(int num) {
        if ( num == 0 )
            return 0;
        return num > 0 ? num : -num;
    }

    public int max(int a, int b) {
        return a > b ? a : b;
    }

    public int min(int a, int b) {
        return a > b ? b : a;
    }

}
